//
//  feedBackViewController.m
//  ZhenPin
//
//  Created by tmosport on 14-7-14.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "feedBackViewController.h"
#import "MBProgressHUD.h"

@interface feedBackViewController ()
@property (strong, nonatomic) IBOutlet UITextView *feedBackTV;
@property (strong, nonatomic) IBOutlet UILabel *placeHodlerLabel;

@end

@implementation feedBackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"反馈";
    [self setNavigationItem];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]
                                             initWithTitle:@"发送"
                                             style:UIBarButtonItemStyleBordered
                                             target:self
                                             action:@selector(toSend)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
}

-(void)setNavigationItem {
    [self.navigationController.navigationBar setBackIndicatorImage:
     [UIImage imageNamed:@"return"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
     [UIImage imageNamed:@"return"]];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}

-(void)leftBarbuttonItemClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)dismissKeyboard {
    [_feedBackTV resignFirstResponder];
}
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

-(NSString *)randomStringWithLength:(int)len {
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length]) % [letters length]]];
    }
    return randomString;
}

- (void)toSend{
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.dimBackground = YES;
    HUD.labelText = @"发送反馈";
    NSString *pre = [self randomStringWithLength:5];
    NSString *content = _feedBackTV.text;
    if ([content length] == 0||[content isEqual:[NSNull null]]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请正确填写反馈内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }
    NSString *email = [NSString stringWithFormat:@"%@@163.com",pre];
    NSDictionary *params = @{@"memberid": _memberId,
                             @"access_token": _token,
                             @"email": email,
                             @"content":content
                             };
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger POST:APIURL@"myzhenpin/saveAdvice.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [HUD showAnimated:YES whileExecutingBlock:^{
            sleep(1);
            HUD.mode = MBProgressHUDModeText;
            HUD.labelText = @"发送成功!";
        }completionBlock:^{
            sleep(1);
            [HUD hide:YES afterDelay:1];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO:接口回传失败日志
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)textViewDidChange:(UITextView *)textView{
    if ([_feedBackTV.text length] == 0) {
        _placeHodlerLabel.hidden = NO;
    }else{
        _placeHodlerLabel.hidden = YES;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
